set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.25)

set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER  arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)


project(YzGD32 C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)

add_compile_options(-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)

add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>)
add_compile_options(-g -gdwarf-2)
#if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
#    message(STATUS "Maximum optimization for speed")
#    add_compile_options(-Ofast)
#elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
#    message(STATUS "Maximum optimization for speed, debug info included")
#    add_compile_options(-Ofast -g)
#elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
#    message(STATUS "Maximum optimization for size")
#    add_compile_options(-Os)
#else ()
#    message(STATUS "Minimal optimization, debug info included")
#    add_compile_options(-Og -g)
#endif ()

include_directories(Firmware/GD32F4xx_standard_peripheral/Include
        Firmware/CMSIS/Include Firmware/CMSIS/GD/GD32F4xx/Include
        Firmware/CMSIS User User/Drive/inc Firmware/FreeRTOS/include
        Firmware/FreeRTOS/portable/GCC/ARM_CM4F
        YzGD32/YzFrame/Inc
        YzGD32/YzCore/Inc
        YzGD32/YzApplication/Inc
        YzGD32/YzCheck/Inc
        YzGD32/YzLib/Inc
        YzGD32/YzLib/YzFatFsCover/Inc
        YzGD32/YzLib/YzFont/Inc
        YzGD32/YzLib/YzSoftPort/Inc
        YzGD32/YzLib/YzDriver/Inc
        YzGD32/YzLib/YzUIPort/Inc
        YzGD32/YzLib/YzMemManager/Inc
        YzGD32/YzLib/YzPeripheral/Inc
        YzGD32/YzLib/YzClock/Inc)
add_definitions(-DUSE_STDPERIPH_DRIVER -DGD32F470)
file(GLOB_RECURSE SOURCES "Firmware/CMSIS/GD/GD32F4xx/Source/GCC/startup_gd32f470zetx.s"
        "Firmware/FreeRTOS/portable/MemMang/heap_5.c"
        "Firmware/FreeRTOS/portable/GCC/ARM_CM4F/*.c"
        "Firmware/FreeRTOS/*.*"
        "User/Drive/src/*.c"
        "User/*.c"
        "User/*.cpp"
        "Firmware/CMSIS/GD/GD32F4xx/Source/*.*"
        "Firmware/GD32F4xx_standard_peripheral/Source/*.*"
        "YzGD32/*.c"
        "YzGD32/*.cpp")

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Firmware/Ld/GD32F470ZETX_FALSH.ld)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -save-temps=obj")

add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
add_link_options(-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard)
add_link_options(-u_printf_float -lc -lm )
# -specs=nosys.specs -lnosys
add_link_options(-T ${LINKER_SCRIPT})
add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})

set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")